<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px">
    <el-divider></el-divider>
    <el-form v-model="row" style="margin: 30px 150px 30px 50px;">
      <el-form-item label="账号" :label-width="labelWidth">
        <el-input v-model="row.account" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="姓名" :label-width="labelWidth">
        <el-input v-model="row.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="角色" :label-width="labelWidth">
        <el-select v-model="row.roleManagementId" multiple placeholder="请选择">
          <el-option
            v-for="(item,key) in data"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="帐号状态" :label-width="labelWidth" prop="resource">
        <el-radio-group v-model="row.accountStatus">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="0">冻结</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="update">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        labelWidth:"70px",
        dialogFormVisible: false,
        title: "编辑",
        row: [],
        idx: 1,
        data:[],
      }
    },
    mounted() {
      this.getData();
    },
    methods: {
      getTitle() {
        if (this.idx == 0) { //idx等于0就是编辑
          this.title = "编辑";
        } else if (this.idx == 1) {
          this.title = "添加"; //idx等于1就是添加
        }
      },
      update(row) {
        console.log(row);
        var row = this.row;
        var roleId = JSON.stringify(row.roleManagementId);  //数组转为字符串
        roleId = roleId.slice(1,roleId.length-1)  //去掉中括号
        //编辑
        if (this.idx == 0) {
          // 请求数据
          this.$axios.request({
            url: this.API + "/administrator/administratorUser/update",
            data: {
              account: row.account,
              accountStatus: row.accountStatus,
              id: row.id,
              name: row.name,
              roleManagementId: roleId
            },
            method: "POST"
          }).then((res) => {
            // console.log(res);
            if (res.data.code == 0) { //编辑成功
              this.dialogFormVisible = false; //关闭窗口
              this.$message.success("编辑成功");
              this.parent.gettableData();
            } else { //编辑失败
              this.$message.error(res.data.msg)
            }
          })
          //添加
        } else if (this.idx == 1) {
          console.log(row)
          this.$axios.request({
            url: this.API + "/administrator/administratorUser/save",
            data: {
              account: row.account,
              accountStatus: row.accountStatus,
              id: row.id,
              name: row.name,
              roleManagementId: roleId
            },
            method: "post"
          }).then((res) => {
            if (res.data.code == 0) { //添加成功
              this.parent.getData();
              this.dialogFormVisible = false; //关闭窗口
              this.$message.success("添加成功");
            } else { //添加失败
              this.$message.error(res.data.msg);
            }
          })
        }
      },
      //获取角色的数据
      getData(){
        this.$axios.get(this.API + "/administrator/roleManagement/list").then((res) => {
          // console.log(res)
          if(res.data.code == 0){
            this.data = res.data.rows;
          }
        })
      },
    },
    props: ['parent']
  }
</script>

<style>
</style>
